package com.wl.shixun5.dao;

import com.wl.shixun5.info.BookInfo;
import com.wl.shixun5.util.Bookjdbc_Util;
import com.wl.shixun5.info.CartInfo;
import com.wl.shixun5.info.CartInfo_BookInfo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 购物车信息DAO类
 */
public class CartInfo_DAO {

    /**
     * 添加图书到购物车
     * @param userId 用户ID
     * @param bookId 图书ID
     * @return 影响的行数
     */
    public static int addcart(String userId, String bookId) {
        int i = 0;
        String sql = "update cartinfo set bookCount=bookCount+1 where userId=? and bookId=?";
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setString(1, userId);
            ps.setString(2, bookId);
            i = ps.executeUpdate();
            if (i == 0) {
                sql = "insert into cartinfo (userId,bookId,bookCount) values (?,?,?)";
                ps = cnn.prepareStatement(sql);
                ps.setString(1, userId);
                ps.setString(2, bookId);
                ps.setInt(3, 1);
                i = ps.executeUpdate();
            }

            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 查询用户购物车中的图书列表
     * @param userId 用户ID
     * @return 购物车图书列表
     */
    public static List<CartInfo_BookInfo> find(String userId) {
        List<CartInfo_BookInfo> li = new ArrayList<CartInfo_BookInfo>();
        String sql = "SELECT * from bookinfo,cartinfo WHERE bookinfo.Id=cartinfo.bookId and userId=?";
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setString(1, userId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                CartInfo ci = new CartInfo();
                BookInfo bi = new BookInfo();
                CartInfo_BookInfo c = new CartInfo_BookInfo(ci, bi);
                bi.setBookAuthor(rs.getString("bookAuthor"));
                bi.setBookISBN(rs.getString("bookISBN"));
                bi.setBookName(rs.getString("bookName"));
                bi.setId(rs.getInt("bookinfo.Id"));
                bi.setIntroduce(rs.getString("introduce"));
                bi.setPrice(rs.getFloat("price"));
                bi.setPublisher(rs.getString("publisher"));
                ci.setId(rs.getInt("cartinfo.Id"));
                ci.setUserId(rs.getString("UserId"));
                ci.setBookCount(rs.getInt("bookCount"));
                ci.setBookId(rs.getString("bookId"));
                li.add(c);
            }
            if (rs != null) {rs.close();}
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return li;
    }

    /**
     * 删除购物车中的图书
     * @param bookId 图书ID
     * @return 影响的行数
     */
    public static int del(String bookId) {
        String sql = "delete from cartinfo where bookId=?";
        int i = 0;
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setString(1, bookId);
            i = ps.executeUpdate();
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return i;
    }

    /**
     * 清空用户购物车
     * @param userId 用户ID
     * @return 影响的行数
     */
    public static int empty(String userId) {
        String sql = "delete from cartinfo where userId=?";
        int i = 0;
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setString(1, userId);
            i = ps.executeUpdate();
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return i;
    }

    /**
     * 修改购物车中图书数量
     * @param userId 用户ID
     * @param bookId 图书ID
     * @param bookCount 图书数量
     * @return 影响的行数
     */
    public static int change(String userId, String bookId, String bookCount) {
        String sql = "update cartinfo set bookCount=? where userId=? and bookId=?";
        int i = 0;
        try {
            Connection cnn = Bookjdbc_Util.getConnection();
            PreparedStatement ps = cnn.prepareStatement(sql);
            ps.setString(1, bookCount);
            ps.setString(2, userId);
            ps.setString(3, bookId);

            i = ps.executeUpdate();
            if (ps != null) {ps.close();}
            if (cnn != null) {cnn.close();}
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return i;
    }
}
